// pages/product-detail/product-detail.js
var WxParse = require('../../wxParse/wxParse.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    type:'', //是否可编辑, update：显示编辑按钮
    showEdit:false, //隐藏编辑
    productList: [
     
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var id = options.id;
    var type = options.type;
    that.setData({
      id, type
    },()=>{
      that.getProductDetail();
    })

    if (type=='update'){
      that.setData({
        showEdit:true
      })
    }
  },

  getProductDetail(){ //获取产品详情
    var that = this;
    wx.request({
      url: app.globalData.host + 'product/productDetail',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      data: {
        id:that.data.id
      },
      success(res) {
        console.log(res)
        if (res.data.data.detail.banner){
          var list = res.data.data.detail.banner
          var list2 = list.split(',')
        }else{
          var list2=[];
        }
        that.setData({
          detail:res.data.data.detail,
          banner: list2,
          member: res.data.data.detail.member,
          productList: res.data.data.more.data,
        })
        var article = that.data.detail.detail;
        WxParse.wxParse('article', 'html', article, that, 5);
      },
      fail(err) { },
      complete(comp) { },
    })

  },


  openProductDetail: function (e) { //打开产品详情
    let id = e.currentTarget.dataset.id;
    console.log(id)
    wx.navigateTo({
      url: '/pages/product-detail/product-detail?id=' + id,
    })
  },

  editTap(){ //编辑产品
    var that =this;
    var id = that.data.id;
    wx.navigateTo({
      url: '/pages/product-add/product-add?id=' + id + '&type=' + 'update',
    })
  },

  deleteTap(){ //删除产品
    var that =this ;
    var id = that.data.id;
    var params = {
      user_id: wx.getStorageSync('userId'),
      id: id,
    };
    wx.showModal({
      title: '提示',
      content: '确定删除此产品吗？',
      success:function(res){
        if(res.confirm){   
          app.request(app.globalData.host + 'product/productDelete', params, 'POST', true,
            function (e) {
              console.log(e)
              var msg = e.data.msg
              if(e.data.code==1){
                msg = '删除成功！'
              }
              wx.showToast({
                title: msg,
                icon:'none',
                duration: 1000,
                success: function(re) {
                },
              })
              setTimeout(function () {
                wx.navigateBack({
                  delta: 1,
                }, 1500)
              })
            },
            function (err) { },
            function (complete) { })
        }
      },
    })

   
  },
  website(){
    var that=this;
    wx.getStorage({
      key: 'userId',
      success (res) {
        var userid=res.data
        if(userid==that.data.member.id){
          wx.navigateTo({
            url: '/pages/home/home',
          })
        }else{
          wx.navigateTo({
            url: '/pages/people-home/people-home?id=' + that.data.member.id,
          })
        }
      }
    })
   
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})